package com.boe.csb.core.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.common.exception.CSBException;
import com.google.gson.JsonObject;

/**
 * 阿里云短信接口
 * Created by tianjin.lp on 16/12/7.
 */
public class AliyunMobileMsgSender implements IMobileMsgSender {

    private static final Logger logger = LoggerFactory.getLogger(AliyunMobileMsgSender.class);

    private String REG_TPL_CODE = "";

    private String REG_SIGN_NAME = "";

    private String PRODUCT_NAME = "京东方";

    private static AliyunMobileMsgSender instance;

    private AliyunMobileMsgSender() {
    }

    public static AliyunMobileMsgSender getInstance() {
    	if(instance == null){
    		instance = new AliyunMobileMsgSender();
            instance.init();
    	}
        
        return instance;
    }

    private void init() {
        REG_TPL_CODE = PropertyUtils.getProperty("reg.tpl.code");
        REG_SIGN_NAME = PropertyUtils.getProperty("reg.sign.name");
    }

    /**
     * 短信发送统一接口   --- 不对外直接使用
     *
     * @param recNum   接收号码
     * @param paramStr 短信模板中的变量 必须是json格式字符串
     * @param signName 控制台创建的签名名称
     * @param tplCode  控制台创建的模板CODE
     * @throws com.aliyuncs.exceptions.ClientException
     */
    private void sendMsg(String recNum, String paramStr, String signName, String tplCode) throws com.aliyuncs.exceptions.ClientException {

        //FIXME 是否校验paramStr json
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIlfrFDD8FsVUS", "H1WJ7FOolt2TxgfPE4H8ZNZU3uDnBF");
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendSmsRequest request = new SingleSendSmsRequest();
        request.setSignName(signName);
        request.setTemplateCode(tplCode);
        request.setParamString(paramStr);
        request.setRecNum(recNum);
        try{
        	SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
            httpResponse.getModel();
        }catch(Exception e){
        	logger.error("", e);
        	throw new CSBException(CSBErrorCode.MOBILE_SEND_ERR, "");
        }
       
    }

    /**
     * 发送注册短信验证码
     *
     * @param code
     * @return
     */
    public boolean sendRegVerifyCode(String code, String recNum) {

        JsonObject job = new JsonObject();
        job.addProperty("code", code);
        job.addProperty("product", PRODUCT_NAME);
        String paramStr = job.toString();

        try {
            sendMsg(recNum, paramStr, REG_SIGN_NAME, REG_TPL_CODE);
        } catch (Exception e) {
            logger.error("send mobile msg err", e);
            return false;
        }

        return true;
    }

    public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException {
        try {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIlfrFDD8FsVUS", "H1WJ7FOolt2TxgfPE4H8ZNZU3uDnBF");
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            request.setSignName("数心科技");
            request.setTemplateCode("SMS_25725802");
            request.setParamString("{\"code\":\"1111\", \"product\":\"数心网络科技\"}");
            request.setRecNum("18600405975");
            SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
            httpResponse.getModel();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
